namespace ui {
	/**
	 * @param title 标题
	 * @param msg string/number[]，'|'符号表示翻页
	 * 输入格式：{"title": "这是标题", "msg":"这是正文|翻页了|再次翻页"}
	 */
	export function msgBook(title: string, msg: string, func: dramaCallback): void {
		const box = new DlgMsgRead(title, msg, func);
		box.show();
	}

	export function msgBookPromise(title: string, msg: string) {
		return new Promise((r) => msgBook(title, msg, r));
	}

	// 竖版显示
	async function playLineVer(lb: eui.Label, text: string, speed: number) {
		if (!text) {
			return 0;
		}

		// let lb2 = new eui.Label;

		const len = text.length;
		for (let i = 1; i <= len; i++) {
			const s = text.substr(0, i);
			lb.text = s;
			await waitMs(speed);
		}
		return 1;
	}

	export class DlgCallback extends PopupPanel {
		cb: dramaCallback;

		override onShow(visible: boolean): void {
			if (!visible) {
				const { cb } = this;
				this.cb = null;
				if (cb) {
					cb(1);
				}
			}
		}
	}

	export class DlgMsgRead extends DlgCallback {
		override btnClose: eui.Button;

		override title: string;

		msgArr: string[] = [];

		ti: eui.Label;

		lbMsg: eui.Label;

		lbPage: eui.Label;

		btnUp: eui.Button;

		btnDown: eui.Button;

		curPage = 0;

		constructor(title: string, msg: string, cb: dramaCallback) {
			super();
			this.skinName = 'pnl_books_item';
			this.title = title;
			this.msgArr = msg.split('|');
			this.cb = cb;
			allCenter(this);
		}

		override childrenCreated(): void {
			this.outsideClose = true;

			this.ti.text = this.title || '';
			console.log(this.msgArr);

			this.bindEvent();
			this.setText();
		}

		bindEvent(): void {
			this.btnClose.addEventListener('touchTap', this.onClose, this);

			this.btnUp.addEventListener(
				'touchTap',
				() => {
					if (this.curPage === 0) {
						ui.msgline('已是第一页');
					} else {
						this.curPage--;
						this.setText();
					}
				},
				this,
			);

			this.btnDown.addEventListener(
				'touchTap',
				() => {
					if (this.curPage >= this.msgArr.length - 1) {
						ui.msgline('已是最后一页');
					} else {
						this.curPage++;
						this.setText();
					}
				},
				this,
			);
		}

		setText(): void {
			this.lbMsg.text = this.msgArr[this.curPage];
			this.lbPage.text = `${this.curPage + 1}/${this.msgArr.length}`;
		}
	}

	export function msgLetter(param: MsgReadParam, cb: dramaCallback): void {
		const letter = new DlgMsgLetter(param, cb);
		letter.show();
	}

	export function msgLetterPromise(param: MsgReadParam) {
		return new Promise((r) => msgLetter(param, r));
	}

	export class DlgMsgLetter extends DlgCallback {
		constructor(param: MsgReadParam, cb: dramaCallback) {
			super();
			this.skinName = 'pnl_letter_item';
			allCenter(this);
			this.from = param.from;
			this.to = param.to;
			this.msg1 = param.col1;
			this.msg2 = param.col2;
			this.msg3 = param.col3;
			this.msg4 = param.col4;
			this.msg5 = param.col5;
			this.msg6 = param.col6;
			this.speed = param.speed || 100;
			this.cb = cb;
		}

		to: string;

		from: string;

		lbTo: eui.Label;

		lbFrom: eui.Label;

		lb1: eui.Label;

		lb2: eui.Label;

		lb3: eui.Label;

		lb4: eui.Label;

		lb5: eui.Label;

		lb6: eui.Label;

		msg1 = '';

		msg2 = '';

		msg3 = '';

		msg4 = '';

		msg5 = '';

		msg6 = '';

		speed = 100;

		override childrenCreated(): void {
			super.childrenCreated();
			this.playDrama();
		}

		async playDrama() {
			const lines = [
				this.lbTo,
				this.lb1,
				this.lb2,
				this.lb3,
				this.lb4,
				this.lb5,
				this.lb6,
				this.lbFrom,
			];
			const msgs = [
				this.to,
				this.msg1,
				this.msg2,
				this.msg3,
				this.msg4,
				this.msg5,
				this.msg6,
				this.from,
			];
			for (let i = 0; i < lines.length; i++) {
				lines[i].text = '';
			}
			if (this.speed < 0) {
				for (let i = 0; i < lines.length; i++) {
					lines[i].text = msgs[i];
				}
			} else {
				for (let i = 0; i < lines.length; i++) {
					const rt = await playLineVer(lines[i], msgs[i], this.speed);
					if (rt) {
						await waitSec(1);
					}
				}
			}
			this.onClose();
		}
	}

	/**
	 * @param msg string
	 * 输入格式：{"msg":"这是正文"}
	 */
	export function msgMupai(msg: string): void {
		console.log('msgread', msg);
		const box = new DlgMsgMupai(msg);
		box.toggle();
	}

	export class DlgMsgMupai extends PopupPanel {
		private static instance: DlgMsgMupai;

		public static ins(msg: string): DlgMsgMupai {
			if (!this.instance) {
				this.instance = new DlgMsgMupai(msg);
			}
			return this.instance;
		}

		override btnClose: eui.Button;

		msg: string;

		lbMsg: eui.Label;

		constructor(msg: string) {
			super();
			this.skinName = 'ui_msgMupai';
			this.msg = msg;
			allCenter(this);
		}

		override childrenCreated(): void {
			this.outsideClose = true;
			this.btnClose.addEventListener('touchTap', this.onClose, this);
			this.lbMsg.text = this.msg || '';
		}
	}
}
